<template>
  <div class="metadata_management_CSGL">
    <div class="pubBig Privilege">
      <div class="h0">
        <p class="lt"></p>
      </div>
      <div class="dDownMain">
        <div
          class="twoBox"
          :style="
            this.$route.query.Token
              ? 'top:50px;left:10px'
              : 'top: 100px;padding:10px'
          "
        >
          <el-row>
            <el-col :span="24">
              <el-button
                style="float:right"
                size="small"
                icon="el-icon-plus"
                @click="open"
                >新增厂商</el-button
              >
              <el-input
                v-model="search_val"
                size="mini"
                style="display:inline-block;width:20%;float:left;margin-right:10px"
                placeholder="请输入厂商名称/厂商认证编码"
              ></el-input>
              <el-button
                icon="el-icon-search"
                size="mini"
                style="float:left"
                @click="(page.page = 1), getTableData()"
              ></el-button>
            </el-col>
          </el-row>
          <template>
            <el-table
              :data="tableData"
              style="width: 100%;margin-top:10px"
              v-loading="isLoading"
              height="600"
            >
              <el-table-column label="序号" type="index"> </el-table-column>
              <el-table-column prop="VendorName" label="厂商名字">
              </el-table-column>
              <el-table-column prop="VendorDesc" label="厂商描述">
              </el-table-column>
              <el-table-column prop="VendorTel" label="联系电话">
              </el-table-column>
              <el-table-column prop="CreateUser" label="创建人">
              </el-table-column>
              <el-table-column label="创建时间" prop="CreateTime">
                <template slot-scope="scope">
                  {{ scope.row.CreateTime | dateFormat }}
                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <!-- <el-button
                    type="text"
                    size="mini"
                  >详情</el-button> -->
                  <el-button
                    size="mini"
                    icon="el-icon-edit"
                    @click="item_edit(scope.row)"
                  ></el-button>
                  <el-button
                    size="mini"
                    icon="el-icon-delete"
                    @click="item_del(scope.row.Id)"
                  ></el-button>
                </template>
              </el-table-column>
            </el-table>
          </template>
          <div class="block">
            <el-pagination
              style="text-align:center"
              layout="prev, pager, next"
              :total="page.total"
              :page-size="page.limit"
              @current-change="page_change"
              :current-page="page.page"
            >
            </el-pagination>
          </div>
        </div>
        <el-dialog
          :title="title"
          :visible.sync="dialogVisible"
          width="45%"
          :before-close="handleClose"
          :append-to-body="true"
          :close-on-click-modal="false"
        >
          <el-form
            label-position="right"
            label-width="120px"
            :model="formLabelAlign"
          >
            <el-form-item
              label="厂商名称"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.VendorName"></el-input>
            </el-form-item>
            <el-form-item
              label="厂商描述"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.VendorDesc"></el-input>
            </el-form-item>
            <el-form-item
              label="联系人电话"
              style="display:inline-block;width:50%"
            >
              <el-input v-model="formLabelAlign.VendorTel"></el-input>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="handleClose">取 消</el-button>
            <el-button type="primary" @click="btn_Ok">保 存</el-button>
          </span>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      tableData: [],
      isLoading: false,
      search_val: "",
      page: {
        page: 1,
        limit: 10,
        total: 1,
      },
      dialogVisible: false,
      formLabelAlign: {},
      title: "",
    };
  },
  methods: {
    getTableData() {
      //获取表格数据
      this.isLoading = true;
      this.$post(
        "/MeatDataManage/Vendor/List?keyword=" +
          this.search_val +
          "&page=" +
          this.page.page +
          "&limit=" +
          this.page.limit
      ).then((res) => {
        if (res.code == 0) {
          this.tableData = res.data.list;
          this.page.total = res.data.TotalCount;
          this.isLoading = false;
        }
      });
    },
    page_change(val) {
      //页码切换
      this.page.page = val;
      this.getTableData();
    },
    item_edit(val) {
      //修改
      var info = JSON.stringify(val);
      this.formLabelAlign = JSON.parse(info);
      // this.dialogVisible = true
      this.open();
    },
    item_del(id) {
      //删除
      this.$confirm("确定删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then((_) => {
          this.$post("/MeatDataManage/Vendor/Delete/" + id)
            .then((res) => {
              if (res.code == 0) {
                this.getTableData();
                this.$message({
                  message: "操作成功",
                  type: "success",
                });
              } else {
                this.$message({
                  message: res.msg,
                  type: "error",
                });
              }
            })
            .catch((err) => {
              this.$message({
                message: res.msg,
                type: "error",
              });
            });
        })
        .catch((_) => {});
    },
    open() {
      //弹层开启
      this.formLabelAlign.Id ? (this.title = "修改") : (this.title = "新增");
      this.dialogVisible = true;
    },
    handleClose() {
      //弹层关闭
      this.dialogVisible = false;
      this.formLabelAlign = {};
    },
    btn_Ok() {
      //弹层确定事件
      let url = this.formLabelAlign.Id
        ? "/MeatDataManage/Vendor/Update"
        : "/MeatDataManage/Vendor/Create";
      let body = this.formLabelAlign;
      this.$post(url, body)
        .then((res) => {
          if (res.code == 0) {
            this.getTableData();
            this.handleClose();
            this.$message({
              message: "操作成功",
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
        .catch((err) => {
          this.$message({
            message: res.msg,
            type: "error",
          });
        });
    },
  },
  mounted() {
    // document.getElementById("2").focus();
  },
  created() {
    if (this.$route.query.Token) {
      localStorage.setItem("Token", this.$route.query.Token);
    }
    this.getTableData();
  },
  filters: {
    dateFormat(val) {
      if (val == null || val == "") {
        return "";
      } else {
        let d = new Date(val); // val 为表格内取到的后台时间
        let month =
          d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1;
        let day = d.getDate() < 10 ? "0" + d.getDate() : d.getDate();
        let hours = d.getHours() < 10 ? "0" + d.getHours() : d.getHours();
        let min = d.getMinutes() < 10 ? "0" + d.getMinutes() : d.getMinutes();
        let sec = d.getSeconds() < 10 ? "0" + d.getSeconds() : d.getSeconds();
        let times =
          d.getFullYear() +
          "-" +
          month +
          "-" +
          day +
          " " +
          hours +
          ":" +
          min +
          ":" +
          sec;
        return times;
      }
    },
  },
};
</script>
<style lang="scss">
.metadata_management_CSGL {
  .top-input {
    display: inline-block;
    width: 350px;
  }

  .box-top {
    margin-top: 15px;
    margin-left: 5px;
  }
}
</style>
